<?php

/**
 * UsersController
 * 
 * Expression cursor is undefined on line 6, column 6 in Templates/Scripting/CakePHPController.php.
 *
 * @category   Controller
 * @package    UsersController
 * @subpackage AppController
 * @copyright  Copyright (c) 2012 Victor Rosset
 * @license    
 * @version    0.1 alpha
 * @link       http://www.tucho235.com.ar
 * @since      25-ago-2012
 * @author     Victor Rosset <victorrosset@gmail.com> @tucho235
 * @php        5.3.13
 * 
 */
 
class UsersController extends AppController
{
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('add','edit');
//        $this->Auth->allow('add', 'logout');
    }

    public function login() {
        $this->layout = 'login';
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Usuario o Contraseña inálido, inténtelo nuevamente'));
            }
        }
        $this->render('/Users/login');
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }

    public function index() {
        $this->User->recursive = 0;
        $this->set('users', $this->paginate());
        $this->render('/Users/index');
        
    }

    public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Usuario inexistente'));
        }
        $this->set('user', $this->User->read(null, $id));
        $this->render('/Users/view');
    }

    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('El Usuario ha sido guardado'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('El Usuario no se ha podido guardar'));
            }
        } else {
            $this->set('roles', $this->roles);
        }
        $this->render('/Users/add');
            
    }

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Usuario inexistente'));
        }
        if (   $this->request->is('post') 
            or $this->request->is('put')) 
        {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('El Usuario ha sido editado'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('El Usuario no se ha podido editar'));
            }
        } else {
            $this->set('roles', $this->roles);
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
        $this->render('/Users/edit');
    }

    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Usuario inexistente'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('El Usuario ha sido eliminado'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('El Usuario no se ha podido eliminar'));
        $this->redirect(array('action' => 'index'));
    }
    
}